/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlynhatre;
import com.db4o.Db4o;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import javax.swing.JOptionPane;
import quanlynhatre.View.*;
import java.io.File;
import quanlynhatre.Model.*;
import com.db4o.config.Configuration;
import com.db4o.config.EmbeddedConfiguration;

/**
 *
 * @author VOTHUY
 */
public class QuanLyNhaTre {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        FrameMain frm = new FrameMain();
        frm.setVisible(true);
        String FileName = "src\\DaTa\\QuanLyNhaTre.db4o";
         EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
        try
        {
            //new File(FileName).delete();to
            
            //config.common().objectClass(NamHoc.class).cascadeOnUpdate(true);
            ObjectContainer db = Db4oEmbedded.openFile(FileName);
            //db.delete( db.queryByExample(o).next() );
            //CreateHocKy(db);
            //PrintHocKy(db);
            //CreateNamHoc(db);
            //PrintNamHoc(db);
            db.close();
        }
        
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public static void CreateHocKy(ObjectContainer db)
    {
        HocKy h1 = new HocKy("1");
        HocKy h2 = new HocKy("2");
        HocKy h3 = new HocKy("He");
        
        db.store(h1);
        db.store(h2);
        db.store(h3);
    }
    
    public static void PrintHocKy(ObjectContainer db)
    {
        ObjectSet result = db.queryByExample(HocKy.class);
        while (result.hasNext())
        {
            HocKy hk = (HocKy)result.next();
            System.out.println(hk.getTenHocKy());
        }
    }
    
    public static void PrintNamHoc(ObjectContainer db)
    {
        ObjectSet result = db.queryByExample(NamHoc.class);
        while (result.hasNext())
        {
            NamHoc hk = (NamHoc)result.next();
            System.out.println(hk.getNamHoc());
        }
    }
    
    public static void CreateNamHoc(ObjectContainer db)
    {
        NamHoc n1 = new NamHoc("2014");
        /*ObjectSet result = db.queryByExample(HocKy.class);
        while (result.hasNext())
        {
            HocKy hk = (HocKy)result.next();
            n1.getHKs().add(hk);
        }*/
        //NamHoc n2 = new NamHoc("2015");
        //NamHoc n3 = new NamHoc("2016");
        db.store(n1);
        
    }
}
